<template>
	<view class="fwcontainer">
		<view class="uni-padding-wrap uni-common-mt">					
			<form @submit="bindSubmit" @reset="bindReset">
				<view class="uni-form-item uni-column">
					<view class="uni-title">旧口令</view>
					<input class="uni-input" name="oldpwd" password="true" placeholder="请输旧口令" maxlength="20" />
				</view>
				<view class="uni-form-item uni-column">
					<view class="uni-title">新口令</view>
					<input class="uni-input" name="newpwd" password="true" placeholder="请输新口令" maxlength="20" />
				</view>
				<view class="uni-form-item uni-column">
					<view class="uni-title">确认口令</view>
					<input class="uni-input" name="newpwd_confirm" password="true" maxlength="20" placeholder="请输入确认口令" />
				</view>
				<button form-type="submit" type="primary" class="submit-button">保存设置</button>
				<!-- <button form-type="reset">重置</button> -->
			</form>
		</view>
	</view>
</template>

<script>
	var  graceChecker = require('@/static/common/graceChecker.js');
	
	const APP = getApp();
	
	export default {
	    components: {},
		data() {
			return {
				//设置数据
				dataRefreshPrepage:false,
			}
		},
		computed:{
			
		},
		methods:{
			bindReset(e){
				console.log(e);
			},
			bindSubmit(e){
				//定义表单规则
				var rule = [
					{name:"oldpwd", checkType : "notnull", checkRule:"",  errorMsg:"请输入旧口令"},
				    {name:"oldpwd", checkType : "string", checkRule:"6,20",  errorMsg:"旧口令必须是6-20位字符"},
				    {name:"newpwd", checkType : "notnull", checkRule:"",  errorMsg:"请输入新口令"},
					{name:"newpwd", checkType : "string", checkRule:"6,20",  errorMsg:"新口令必须是6-20位字符"},
					{name:"newpwd_confirm", checkType : "compare", checkRule:"newpwd",  errorMsg:"确认口令与新口令不一致"}				
				];
				//进行表单检查
				var formData = e.detail.value;
				var checkRes = graceChecker.check(formData, rule);
				if(checkRes){
					let _this = this;
					APP._post('/api/owner/resetpwd', {
						oldpwd:formData.oldpwd,
						newpwd:formData.newpwd,
						newpwd_confirm:formData.newpwd_confirm
					}, null, function (res) {						
					  //执行成功
					  if (res.code === APP.appconfig.fwcode.successed) {
						APP.apptool.showToastInfo(res.msg, null, 2000, function (res) {
							uni.switchTab({
								url:'/pages/owner/owner'
							});
						});
					  } else {
						APP.apptool.showToastInfo(res.msg);
					  }
					}, function (res) {
					  //执行失败
					}, function (res) {
					  //执行完成
					});
				}else{
				    uni.showToast({ title: graceChecker.error, icon: "none" });
				}
			}
		},
		onLoad(options) {
			if (options.refreshpage){
				//刷新标识：refreshpage=refresh
				this.dataRefreshPrepage = (options.refreshpage=='refresh');
			}
		}
	}
</script>

<style>
	.submit-button{margin-top: 20px;}
</style>

